package stylistcrm.core.services.order.exception;

public class BizException extends Exception{
	
  private static final long serialVersionUID = 3474261298168855986L;
  private static final String DEFAULT_ERRCODE = "5000";
  private String errCode;

  public BizException(MessageCode messageCode){
    super(messageCode.message());
    setErrCode(messageCode);
  }

  public BizException(MessageCode messageCode, Throwable cause) {
    super(messageCode.message(), cause);
    setErrCode(messageCode);
  }

  public BizException(String code, String message) {
    super(message);
    setErrCode(code);
  }

  public BizException(String message) {
    super(message);
    setErrCode(DEFAULT_ERRCODE);
  }

  private void setErrCode(MessageCode messageCode) {
    setErrCode(messageCode.code());
  }

  private void setErrCode(String code) {
    this.errCode = code;
  }

  public static boolean isBizException(String code){
    return code.indexOf("_") != -1;
  }

  public void recover() {
    int pos = this.errCode.indexOf("_");
    if (pos != -1)
      this.errCode = this.errCode.substring(pos + 1);
  }

  public String code(){
    return this.errCode;
  }

  public String message(){
    return getMessage();
  }
}
